/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ws;

import java.io.StringReader;
import java.util.ArrayList;
import javax.servlet.ServletResponseWrapper;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * REST Web Service
 *
 * @author galen
 */
@Path("article")
public class Article {
    
    
    @Context
    private UriInfo context;

    /** Creates a new instance of Article */
    public Article() {

    }
    

    /**
     * Gets list of available categories
     * @return XML with the categories
     */
    @GET
    @Produces("application/xml")
    @Path("/categories")
    public String getXmlCategoryList() {
        String catXML = listCategories();
        System.out.println(catXML);
        return catXML;
    }

    
    /**
     * Gets list of available articles from a specific category
     * @param catName the category that should be browsed
     * @return XML string with articles
     */
    @GET
    @Produces("application/xml")
    @Path("/categories/{catName}")
    public String getXmlCategoryArticle(@PathParam("catName") String catName) {
        String catXML = "<category>"+catName+"</category>";
        String catIdXML = browseCategory(catXML);
        System.out.println(catIdXML);
        return catIdXML;

    }

    /**
     * Browse the content of a specific article
     * @param articleID the id of the article
     * @return XML string with the article content
     */
    @GET
    @Produces("application/xml")
    @Path("/articleid/{articleID}")
    public String getXmlArticle(@PathParam("articleID") String articleID) {
        
        String articleXML = getArticle("<id>"+articleID+"</id>");
        System.out.println(articleXML);
        return articleXML;

    }  

    /**
     * PUT method for updating or creating an instance of Article
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }

    private static String listCategories() {
        ws.NewsServer_Service service = new ws.NewsServer_Service();
        ws.NewsServer port = service.getNewsServerPort();
        return port.listCategories();
    }

    private static String browseCategory(java.lang.String category) {
        ws.NewsServer_Service service = new ws.NewsServer_Service();
        ws.NewsServer port = service.getNewsServerPort();
        return port.browseCategory(category);
    }

    private static String listArticles(java.lang.String count) {
        ws.NewsServer_Service service = new ws.NewsServer_Service();
        ws.NewsServer port = service.getNewsServerPort();
        return port.listArticles(count);
    }

    private static String getArticle(java.lang.String id) {
        ws.NewsServer_Service service = new ws.NewsServer_Service();
        ws.NewsServer port = service.getNewsServerPort();
        return port.getArticle(id);
    }

}
